package com.ruoyi.web.controller.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.web.controller.demo.domain.CarList;
import com.ruoyi.web.controller.mapper.CarProcessMapper;
import com.ruoyi.web.controller.demo.domain.CarProcess;
import com.ruoyi.web.controller.service.ICarProcessService;
import com.ruoyi.common.core.text.Convert;

/**
 * 车辆申请Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-26
 */
@Service
public class CarProcessServiceImpl implements ICarProcessService 
{
    @Autowired
    private CarProcessMapper carProcessMapper;

    /**
     * 查询车辆申请
     * 
     * @param processId 车辆申请主键
     * @return 车辆申请
     */
    @Override
    public CarProcess selectCarProcessByProcessId(Long processId)
    {
        return carProcessMapper.selectCarProcessByProcessId(processId);
    }

    @Override
    public int updateCarStatus(Long usedCar) {
        return carProcessMapper.updateCarStatus(usedCar);
    }

    /**
     * 查询车辆申请列表
     * 
     * @param carProcess 车辆申请
     * @return 车辆申请
     */
    @Override
    public List<CarProcess> selectCarProcessList(CarProcess carProcess)
    {
        return carProcessMapper.selectCarProcessList(carProcess);
    }

    /**
     * 新增车辆申请
     * 
     * @param carProcess 车辆申请
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCarProcess(CarProcess carProcess)
    {
        int rows = carProcessMapper.insertCarProcess(carProcess);
        insertCarList(carProcess);
        return rows;
    }

    /**
     * 修改车辆申请
     * 
     * @param carProcess 车辆申请
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCarProcess(CarProcess carProcess)
    {
        carProcessMapper.deleteCarListByCarId(carProcess.getProcessId());
        insertCarList(carProcess);
        return carProcessMapper.updateCarProcess(carProcess);
    }

    /**
     * 批量删除车辆申请
     * 
     * @param processIds 需要删除的车辆申请主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCarProcessByProcessIds(String processIds)
    {
        carProcessMapper.deleteCarListByCarIds(Convert.toStrArray(processIds));
        return carProcessMapper.deleteCarProcessByProcessIds(Convert.toStrArray(processIds));
    }

    /**
     * 删除车辆申请信息
     * 
     * @param processId 车辆申请主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCarProcessByProcessId(Long processId)
    {
        carProcessMapper.deleteCarListByCarId(processId);
        return carProcessMapper.deleteCarProcessByProcessId(processId);
    }

    /**
     * 新增车辆列信息
     * 
     * @param carProcess 车辆申请对象
     */
    public void insertCarList(CarProcess carProcess)
    {
        List<CarList> carListList = carProcess.getCarListList();
        Long processId = carProcess.getProcessId();
        if (StringUtils.isNotNull(carListList))
        {
            List<CarList> list = new ArrayList<CarList>();
            for (CarList carList : carListList)
            {
                carList.setCarId(processId);
                list.add(carList);
            }
            if (list.size() > 0)
            {
                carProcessMapper.batchCarList(list);
            }
        }
    }
}
